//1.创建舞台、场景

$(document).keyup(function (event) {
    keyListener(event);
});

//转折点
var nodeList = [];
var nodeSnake = {
    throughPointList: [],
    direction: RIGHT,
    size: {w: lengthPerTime, h: lengthPerTime},
    location: {x: 100, y: 100}
};
var canvas = document.getElementById('canvas');
var WIDTH = canvas.width;
var HEIGHT = canvas.height;
var stage = new JTopo.Stage(canvas); // 创建一个舞台对象
var scene = new JTopo.Scene(stage); // 创建一个场景对象
var redirectList = [];
var lengthPerTime = 10;
var lastKey = 0;
var LEFT = -1;
var RIGHT = 1;
var UP = 2;
var DOWN = -2;
var food = null;
var speed = 100;//毫秒
createNodes(30, 100, 100, RIGHT);
addRandomNode();

//监听按键
function keyListener(event) {
    var keyCode = event.keyCode;
    switch (keyCode) {
        case 37:
            addRedirectPoint(nodeList[nodeList.length - 1].x, nodeList[nodeList.length - 1].y, LEFT);
            break;
        case 38:
            addRedirectPoint(nodeList[nodeList.length - 1].x, nodeList[nodeList.length - 1].y, DOWN);
            break;
        case 39:
            addRedirectPoint(nodeList[nodeList.length - 1].x, nodeList[nodeList.length - 1].y, RIGHT);
            break;
        case 40:
            addRedirectPoint(nodeList[nodeList.length - 1].x, nodeList[nodeList.length - 1].y, UP);
            break;
    }
}
//移动定时器
var startId = setInterval(moveSnake, speed);

//蛇开始移动
function moveSnake() {
    for (var index = nodeList.length - 1; index >= 0; index--) {
        var $node = nodeList[index];
        var flag = (index == 0);
        moveNode(lengthPerTime, $node, flag);
    }
}
//添加转折点
function addRedirectPoint(x, y, direction) {
    var headNode = nodeList[nodeList.length - 1];
    var nowDirection = headNode.direction;
    if (Math.abs(nowDirection + direction) % 2 != 1) {
        return false;
    }
    for (var index2 = 0; index2 < redirectList.length; index2++) {
        if (!redirectList[index2].aborted && redirectList[index2].x == x && redirectList[index2].y == y) {
            return false;
        }
    }
    redirectList.push({
        redirectX: x,
        redirectY: y,
        redirection: direction,
        aborted: false
    });
    lastKey = direction;
}

//是否撞墙了
function isFailed(x, y) {
    var cnt = 0;
    if (x < 0 || y < 0 || x>WIDTH-10 || y>HEIGHT-10) {
        return true;
    }
    for (var index = 0; index < nodeList.length; index++) {
        var tempX = nodeList[index].x;
        var tempY = nodeList[index].y;
        if (tempX == x && y == tempY) {
            cnt++;
        }
    }
    return cnt > 1 ? true : false;
}

//创建节点
function createNodes(num, oriX, oriY, oriDirecttion) {
    for (var index = 0; index < num; index++) {
        var node = new JTopo.Node();
        node.setLocation(oriX + lengthPerTime * (index), oriY);
        node.setSize(lengthPerTime, lengthPerTime);
        node.textPosition = "Bottom_Center";
        node.direction = oriDirecttion;
        node.redirectIndex = 0;
        nodeList.push(node);
        scene.add(node);
    }
}

//添加随机节点
function addRandomNode() {
    food = new JTopo.Node();
    food.fillColor = "0,0,0";
    food.setSize(lengthPerTime, lengthPerTime);
    food.setLocation(parseInt(Math.random() * 10) * 50, parseInt(Math.random() * 10) * 50);
    food.food = true;
    scene.add(food);
}

//吃
function eatNode(x, y) {
    if (food.x == x && food.y == y) {
        appendNode();
        scene.remove(food);
        food = null;
        addRandomNode();
    }
}


//往蛇添加节点
function appendNode() {
    var node = nodeList[nodeList.length - 1];
    var newNode = new JTopo.Node();
    var x = node.x;
    var y = node.y;
    var direction = node.direction;
    var newX = x;
    var newY = y;
    switch (direction) {
        case UP:
            var newY = y + lengthPerTime;
            break;
        case DOWN:
            var newY = y - lengthPerTime;
            break;
        case LEFT:
            var newX = x - lengthPerTime;
            break;
        case RIGHT:
            var newX = x + lengthPerTime;
            break;
    }
    newNode.redirectIndex = node.redirectIndex;
    newNode.direction = direction;
    newNode.setSize(lengthPerTime, lengthPerTime);
    newNode.textPosition = node.textPosition;
    newNode.setLocation(newX, newY);
    nodeList.push(newNode);
    scene.add(newNode);
}

//移动节点
function moveNode(length, node, lastFlag) {
    var x = node.x;
    var y = node.y;
    var redirectIndex = node.redirectIndex;
    if (redirectList.length > 0 && redirectList[redirectIndex]) {
        var redirectX = redirectList[redirectIndex].redirectX;
        var redirectY = redirectList[redirectIndex].redirectY;
        var redirection = redirectList[redirectIndex].redirection;
        if (redirectX == x && redirectY == y) {
            node.direction = redirection;
            node.redirectIndex++;
        }
    }
    var direction = node.direction;
    var newY = y;
    var newX = x;
    switch (direction) {
        case UP:
            newY = y + length;
            break;
        case DOWN:
            newY = y - length;
            break;
        case LEFT:
            newX = x - length;
            break;
        case RIGHT:
            newX = x + length;
            break;
    }
    node.setLocation(newX, newY);
    redirectList.aborted = lastFlag;
    eatNode(newX, newY);
    if (isFailed(newX, newY)) {
        console.log("failed");
        clearInterval(startId);
    }
}
